<template>
	<view>
		<uni-popup ref="popup" type="center" border-radius="10px">
			<view class="gl-container" style="background-color: #f1f1f1;">
				<view class="gl-header">
					<view class="gl-header-left">
						<text>{{title}}</text>
					</view>
					<view class="gl-header-right">
						<uni-icons type="closeempty" size="16" color="#fff" class="gl-button-icon" @tap="eventClose"
							style="background-color: #dd524d;padding: 2px;border-radius: 2px;"></uni-icons>
					</view>
				</view>
				<view class="gl-body" style="display: flex;flex-direction: column;gap: 10px;padding: 10px;">
					<uni-easyinput type="text" v-model="curData.name">
						<template #left>
							<view class="gl-item-title">名称:</view>
						</template>
					</uni-easyinput>
					<uni-easyinput type="text" v-model="curData.mode">
						<template #left>
							<view class="gl-item-title">关键字:</view>
						</template>
					</uni-easyinput>
					<uni-easyinput type="text" v-model="curData.url">
						<template #left>
							<view class="gl-item-title">URL:</view>
						</template>
					</uni-easyinput>
					<uni-easyinput type="textarea" v-model="curData.content">
						<template #left>
							<view class="gl-item-title">模板:</view>
						</template>
					</uni-easyinput>
				</view>
				<view class="gl-footer" style="gap: 10px; padding: 10px;">
					<view class="gl-button gl-button-base" @tap="eventSubmit">
						<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">保存</text>
					</view>
					<view class="gl-button gl-button-warning" @tap="eventClose">
						<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">取消</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				title: '群组消息模板',
				sConf: getApp().globalData,
				curData: null,
			}
		},
		methods: {
			open(e) {
				this.curData = e
				this.$refs.popup.open()
			},
			eventClose() {
				this.$refs.popup.close()
			},
			eventSubmit() {
				console.log('eventSubmit');
				if (!this.curData.name) {
					uni.showToast({
						title: '模板名称不能空'
					})
					return
				}
				let mid = this.curData.id
				this.loadMailPatch(mid).then(res => {
					console.log('更新模板', res);
					this.$emit('refresh')
					this.eventClose()
				}).catch(err => {
					uni.showModal({
						title: '修改失败'
					})
				})
			},
			loadMailPatch(e) {
				let host = this.sConf.host + '/admin/chatMail/' + e + '/';
				let token = this.sConf.auth.token || '';
				let data = this.curData
				return new Promise((resolve, reject) => {
					psMethod.authenticatorPatchSync(host, token, data).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.gl-item-title {
		padding-left: 5px;
		color: #007aff;
		font-weight: bold;
	}
</style>